package com.fubowen.lepton.extension

import androidx.recyclerview.widget.SortedList
import com.fubowen.lepton.bean.BookmarkBean

fun <T> SortedList<T>.forEach(callback: (T) -> Unit) {
    for (i in 0 until this.size()) {
        val item = this.get(i)
        callback(item)
        this.updateItemAt(i, item)
    }
}

fun <T> SortedList<T>.any(callback: (T) -> Boolean): Boolean {
    for (i in 0 until this.size()) {
        val item = this.get(i)
        if(callback(item)){
            return true
        }
    }
    return false
}

fun <T> SortedList<T>.remove(callback: (T) -> Boolean) {
    for (i in 0 until this.size()) {
        val item = this.get(i)
        if(callback(item)){
            this.removeItemAt(i)
        }
    }
}


fun <T, E> SortedList<T>.map(callback: (T) -> E): ArrayList<E> {
    val list = ArrayList<E>()
    for (i in 0 until this.size()) {
        val item = this.get(i)
        list.add(callback(item))
    }
    return list
}

fun <T> SortedList<T>.filter(callback: (T) -> Boolean): ArrayList<T> {
    val list = ArrayList<T>()
    for (i in 0 until this.size()) {
        val item = this.get(i)
        if (callback(item)) {
            list.add(item)
        }
    }
    return list
}